package com.example.entity;

/**
 * @Author: wanqian
 * @Date: 2020/8/5 17:44
 */
public class ArrayStack {

    private int top = -1;
    private int[] arr;
    private int size;

    public ArrayStack(int size) {
        this.size = size;
        this.arr = new int[size];
    }

    public boolean isFull() {
        return this.top == (size - 1);
    }

    public boolean isEmpty() {
        return this.top == -1;
    }

    public void push(int i) {
        if (isFull()) {
            System.out.println("栈已满");
            return;
        }
        top++;
        arr[top] = i;
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈已空");
        }
        int value = arr[top];
        top--;
        return value;
    }

    public void list() {
        if (isEmpty()) {
            System.out.println("栈已空");
            return;
        }

        for (int i = top; i > -1; i--) {
            System.out.printf("stack[%d]=%d", i, arr[i]);
            System.out.println();
        }
    }
}
